package com.lazy.es.common;

import java.net.InetSocketAddress;

import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 对 elastic 数据库的抽象
 * @author luodan
 *
 */
public class AbstractEsClient {
    protected static final Logger logger = LoggerFactory.getLogger(AbstractEsClient.class);
    
    protected Client client;
    
    public AbstractEsClient(String clusterName){
        Settings settings = Settings.settingsBuilder()
                .put("cluster.name", clusterName).build();
        client = TransportClient.builder().settings(settings).build();
    }
    
    public AbstractEsClient addTransportAddress(String host, int port) {
        ((TransportClient)client).addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress(host, port)));
        return this;
    }
    
    public Client getClient(){
        return client;
    }
    
    public void destroy() {
        if (client != null) {
            client.close();
            client = null;
        }
    }
}
